package com.ledtechdesign.afcsdemo;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import com.ledtechdesign.afcs.AccountManager;
import com.ledtechdesign.beans.Account;
import com.ledtechdesign.service.ServiceManagerEnum;
import com.ledtechdesign.service.Utils;

public class DemoAppManager {
	
	private final static DemoAppManager _instance = new DemoAppManager();
	private  Map<String, AccountManager> _managers;
	
	private static Properties prop;

	static {
		
		try {
			prop = Utils.loadProperties("afcsje_demo.properties");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	

	public DemoAppManager() {
		super();
		// TODO Auto-generated constructor stub
		_managers = new HashMap<String, AccountManager>();
	}

	public  AccountManager getAccountManager(Account acnt,
			ServiceManagerEnum type) throws Exception {
		AccountManager am;
			
		synchronized (_managers) {
			if (!_managers.containsKey(acnt.getAccountName())) {
				try {
					am = new AccountManager(prop,acnt, type);
					_managers.put(acnt.getAccountName(), am);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		am = _managers.get(acnt.getAccountName());
		
		am.login();
		
		return am;
	}

	public static DemoAppManager getInstance() {
		return DemoAppManager._instance;
	}

	public void flushAccounts() {
		_managers.clear();
		_managers = new HashMap<String, AccountManager>();
	}

//	public boolean initAccount(String accountURL) {
//		if(this._managers.containsKey(accountURL)){
//			this._managers.get(accountURL).do_initialize(authToken)
//		}
//		return false;
//	}

}
